#include "X12_REVEIL_lib.h"

void DATE_readHour (unsigned char *heures);
void DATE_readMinute (unsigned char *minutes);
void DATE_writeMinute (unsigned char minutes);
void DATE_writeHour (unsigned char heures);
void DATE_writeSeconde (unsigned char secondes);
void DATE_readSeconde (unsigned char *secondes);

/***** Main de test des fontions ********/

void DATE_writeHour (unsigned char heures)
{
  unsigned char uc_heures;
  uc_heures=bin2bcd(heures);
  I2C_write_byte (RTC_ADDRESS,4,&uc_heures);  
}


void main (void)
{
  unsigned char heures=11, minutes=57, secondes=45, tmp_h, tmp_min, tmp_sec;
  
  X12_init();
  IHM_clear();
  I2C_read_byte (RTC_ADDRESS,3,&minutes);
  while(1);
  
  DATE_writeHour(12);
  DATE_writeMinute(minutes);
  DATE_writeSeconde(secondes);
  while (1)
  {
    
    DATE_readHour(&tmp_h);
    DATE_readMinute(&tmp_min);
    DATE_readSeconde(&tmp_sec);
    
    IHM_gotoxy(0,0);
    IHM_printf("%d:%d:%d", tmp_h, tmp_min, tmp_sec);
  }
}

/******** Fonctions Lecture *********/

void DATE_readHour (unsigned char *heures)
{
  unsigned char tmp; 
  
  I2C_read_byte (RTC_ADDRESS,4,heures);
  *heures= *heures & 0x3F;
  tmp=*heures;
  *heures=bcd2bin (tmp);  
}

void DATE_readMinute (unsigned char *minutes)
{
  unsigned char tmp;
  
  I2C_read_byte (RTC_ADDRESS,3,minutes);
  *minutes= *minutes & 0x7F;
  tmp= *minutes; 
  *minutes=bcd2bin (tmp); 
}

void DATE_readSeconde (unsigned char *secondes)
{
  unsigned char tmp;
  
  I2C_read_byte (RTC_ADDRESS,2,secondes);
  *secondes= *secondes & 0x7F;
  tmp= *secondes;
  *secondes=bcd2bin (tmp);  
}

/*void DATE_readDay (unsidned char *jours)
{
  I2C_read_byte (RTC_ADDRESS,5,jours);
  *jours = *jours & 0x3F;
}

void DATE_readWeekDay (unsidned char *Joursemaine)
{
  I2C_read_byte (RTC_ADDRESS,6,Joursemaine);
  *Joursemaine = *Joursemaine & 0x07;
}

void DATE_readMonth (unsidned char *mois)
{
  I2C_read_byte (RTC_ADDRESS,7,mois);
  *mois = *mois & 0x1F;
}

void DATE_readYears (unsidned char *siecle, unsigned char *annees)
{
  I2C_read_byte (RTC_ADDRESS,7,siecle);
  *siecle = *siecle & 0x80;
  I2C_read_byte(RTC_ADDRESS,8, annees);
} */
/******** Fonctions Ecriture ********/

void DATE_writeMinute (unsigned char minutes)
{
  minutes=bin2bcd (minutes);
  I2C_write_byte (RTC_ADDRESS,3,&minutes);  
}



void DATE_writeSeconde (unsigned char secondes)
{
  secondes=bin2bcd (secondes);
  I2C_write_byte (RTC_ADDRESS,2,&secondes);  
}

/*void DATE_writeDay (unsigned char jours)
{
  I2C_write_byte (RTC_ADDRESS,5,&jours);  
}

void DATE_writeWeekDay (unsigned char Joursemaine)
{
  I2C_write_byte (RTC_ADDRESS,6,&Joursemaine);  
}

void DATE_writeMonth (unsigned char mois)
{
  I2C_write_byte (RTC_ADDRESS,7,&mois);  
}

void DATE_writeYears (unsigned char siecle, unsigned char *annees )
{
  I2C_write_byte (RTC_ADDRESS,7,&siecle);
  I2C_write_byte (RTC_ADDRESS,8,&annees);  
} */
